//
//  MyCoinTopView.m
//  JPWords
//
//  Created by 梁淑婷 on 2024/5/30.
//

#import "MyCoinTopView.h"
#import "CoinDetialView.h"



@interface MyCoinTopView()

@property(nonatomic,strong)UILabel *balanceLabel;
@property(nonatomic,strong)UILabel *dayLabel;
@property(nonatomic,strong)UIImageView *coinImageView;
@property(nonatomic,strong)UILabel *coinLabel;
@property(nonatomic,strong)UIView *progressLine;
@property(nonatomic,strong)CoinDetialView *detialView;
@property(nonatomic,strong)UIImageView *topBgImageView2;
@property(nonatomic,strong)UIImageView *logoImageView;
@property(nonatomic,strong)UILabel *tipLabel;
@property(nonatomic,strong)UIView *bgView;

@end

@implementation MyCoinTopView

-(instancetype)initWithFrame:(CGRect)frame andCurrentAvailableHeight:(CGFloat)currentAvailableHeight{
    self = [super initWithFrame:frame];
    if(self){
        self.currentAvailableWidth = frame.size.width;
        self.currentAvailableHeight = currentAvailableHeight;
        [self createUI];
    }
    return self;
}

-(void)createUI{
    
    __weak typeof(self)self_weak_ = self;
    self.backgroundColor = [UIColor clearColor];
    
    CGFloat h = 140*(self.currentAvailableWidth-32)/375;
    UIImageView *topBgImageView2 = [[UIImageView alloc]init];
    topBgImageView2.image = [UIImage imageNamed:@"mine_point_bg2"];
    topBgImageView2.userInteractionEnabled = YES;
    [self addSubview:topBgImageView2];
    [topBgImageView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(@16);
        make.right.mas_equalTo(@-16);
        make.height.mas_equalTo(h);
        make.bottom.mas_equalTo(-54);
    }];
    self.topBgImageView2 = topBgImageView2;
    
    CGFloat w94 = [DeviceUtils getAdaptedWidthForDesignWidth:94 andCurrentAvailableWidth:self.currentAvailableWidth];
    CGFloat w96 = [DeviceUtils getAdaptedWidthForDesignWidth:96 andCurrentAvailableWidth:self.currentAvailableWidth];
    CGFloat w47 = [DeviceUtils getAdaptedWidthForDesignWidth:47 andCurrentAvailableWidth:self.currentAvailableWidth];
    CGFloat w90 = [DeviceUtils getAdaptedWidthForDesignWidth:90 andCurrentAvailableWidth:self.currentAvailableWidth];
    CGFloat w20 = [DeviceUtils getAdaptedWidthForDesignWidth:20 andCurrentAvailableWidth:self.currentAvailableWidth];
    CGFloat w6 = [DeviceUtils getAdaptedWidthForDesignWidth:6 andCurrentAvailableWidth:self.currentAvailableWidth];
    UIImageView *logoImageView = [[UIImageView alloc]init];
    logoImageView.image = [UIImage imageNamed:@"mine_point_coin_logo"];
    logoImageView.userInteractionEnabled = YES;
    [self addSubview:logoImageView];
    [logoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(topBgImageView2.mas_right);
        make.width.mas_equalTo(w94);
        make.height.mas_equalTo(w96);
        make.bottom.mas_equalTo(topBgImageView2.mas_top).offset(w47);
    }];
    self.logoImageView = logoImageView;
    
    CGFloat d_top = (w47-19)/2.0;
    self.dayLabel = [[UILabel alloc]init];
    self.dayLabel.attributedText = [self changeStr:@"0"];
    [topBgImageView2 addSubview:self.dayLabel];
    CGSize d_size = [self.dayLabel sizeThatFits:CGSizeMake(MAXFLOAT,19)];
    [self.dayLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(@16);
        make.top.mas_equalTo(d_top);
        make.width.mas_equalTo(d_size.width+6);
        make.height.mas_equalTo(@19);
    }];
    self.coinImageView = [[UIImageView alloc]init];
    self.coinImageView.image = [UIImage imageNamed:@"mine_point_point_logo"];
    [topBgImageView2 addSubview:self.coinImageView];
    [self.coinImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(@14);
        make.left.mas_equalTo(self_weak_.dayLabel.mas_right);
        make.centerY.mas_equalTo(self_weak_.dayLabel);
    }];
    self.coinLabel = [[UILabel alloc]init];
    self.coinLabel.font = JPUIFontInterMedium(16);
    self.coinLabel.textColor = KFontBlackColor;
    [topBgImageView2 addSubview:self.coinLabel];
    [self.coinLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self_weak_.coinImageView.mas_right).offset(3);
        make.right.mas_equalTo(-w90);
        make.height.mas_equalTo(@19);
        make.centerY.mas_equalTo(self_weak_.coinImageView);
    }];
    
    self.balanceLabel = [[UILabel alloc]init];
    self.balanceLabel.font = JPUIFontInterSemiBold(28);
    self.balanceLabel.textColor = KFontBlackColor;
    self.balanceLabel.userInteractionEnabled = YES;
    [self addSubview:self.balanceLabel];
    [self.balanceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(@24);
        make.height.mas_equalTo(@34);
        make.bottom.mas_equalTo(topBgImageView2.mas_top).offset(-w20);
        make.right.mas_equalTo(logoImageView.mas_left).offset(-16);
    }];
    UITapGestureRecognizer *btap = [[UITapGestureRecognizer alloc]initWithActionBlock:^(id  _Nonnull sender) {
        [self_weak_ showDetialView];
    }];
    [self.balanceLabel addGestureRecognizer:btap]; 
    
    UILabel *tipLabel = [[UILabel alloc]init];
    tipLabel.font = JPUIFontPingFangSCRegular(14);
    tipLabel.textColor = KFontGrayColor;
    tipLabel.text = @"岛币余额";
    [self addSubview:tipLabel];
    [tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(@24);
        make.height.mas_equalTo(@20);
        make.bottom.mas_equalTo(self_weak_.balanceLabel.mas_top).offset(-w6);
    }];
    self.tipLabel = tipLabel;
    
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(16,self.height-38,self.currentAvailableWidth-32,39)];
    bgView.backgroundColor = [UIColor whiteColor];
    [self addSubview:bgView];
    [bgView addRoundedCorners:UIRectCornerTopLeft|UIRectCornerTopRight withRadii:CGSizeMake(10,10)];
    
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.font = JPUIFontPingFangSCMedium(16);
    titleLabel.textColor = KFontBlackColor;
    titleLabel.text = @"每日任务";
    [bgView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(@16);
        make.top.mas_equalTo(@16);
        make.height.mas_equalTo(@22);
    }];
    
    self.bgView = bgView;
    
}

#pragma mark - 屏幕宽度改变
-(void)refreshWidth{
    __weak typeof(self)self_weak_ = self;
    CGFloat h = 140*(self.currentAvailableWidth-32)/375;
    [self.topBgImageView2 mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(h);
    }];
    CGFloat w94 = [DeviceUtils getAdaptedWidthForDesignWidth:94 andCurrentAvailableWidth:self.currentAvailableWidth];
    CGFloat w96 = [DeviceUtils getAdaptedWidthForDesignWidth:96 andCurrentAvailableWidth:self.currentAvailableWidth];
    CGFloat w47 = [DeviceUtils getAdaptedWidthForDesignWidth:47 andCurrentAvailableWidth:self.currentAvailableWidth];
    CGFloat w90 = [DeviceUtils getAdaptedWidthForDesignWidth:90 andCurrentAvailableWidth:self.currentAvailableWidth];
    CGFloat w20 = [DeviceUtils getAdaptedWidthForDesignWidth:20 andCurrentAvailableWidth:self.currentAvailableWidth];
    CGFloat w6 = [DeviceUtils getAdaptedWidthForDesignWidth:6 andCurrentAvailableWidth:self.currentAvailableWidth];
    [self.logoImageView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self_weak_.topBgImageView2.mas_right);
        make.width.mas_equalTo(w94);
        make.height.mas_equalTo(w96);
        make.bottom.mas_equalTo(self_weak_.topBgImageView2.mas_top).offset(w47);
    }];
    CGFloat d_top = (w47-19)/2.0;
    [self.dayLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(d_top);
    }];
    [self.coinImageView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self_weak_.dayLabel);
    }];
    [self.coinLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self_weak_.coinImageView.mas_right).offset(3);
        make.right.mas_equalTo(-w90);
        make.centerY.mas_equalTo(self_weak_.coinImageView);
    }];
    [self.balanceLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self_weak_.topBgImageView2.mas_top).offset(-w20);
        make.right.mas_equalTo(self_weak_.logoImageView.mas_left).offset(-16);
    }];
    [self.tipLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self_weak_.balanceLabel.mas_top).offset(-w6);
    }];
    
    [self.bgView removeFromSuperview];
    self.bgView = nil;
    
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(16,self.height-38,self.currentAvailableWidth-32,39)];
    bgView.backgroundColor = [UIColor whiteColor];
    [self addSubview:bgView];
    [bgView addRoundedCorners:UIRectCornerTopLeft|UIRectCornerTopRight withRadii:CGSizeMake(10,10)];
    
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.font = JPUIFontPingFangSCMedium(16);
    titleLabel.textColor = KFontBlackColor;
    titleLabel.text = @"每日任务";
    [bgView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(@16);
        make.top.mas_equalTo(@16);
        make.height.mas_equalTo(@22);
    }];
    
    self.bgView = bgView;
    
    for(UIView *subview in self.topBgImageView2.subviews){
        if(!([subview isEqual:self.coinImageView]||[subview isEqual:self.coinLabel]||[subview isEqual:self.dayLabel])){
            [subview removeFromSuperview];
        }
    }
    self.isCreate = NO;
    [self createYearTaskUI];
    
    if(self.detialView){
        self.detialView.frame = CGRectMake(0, 0, self.currentAvailableWidth, self.currentAvailableHeight);
        self.detialView.currentAvailableWidth = self.currentAvailableWidth;
        self.detialView.currentAvailableHeight = self.currentAvailableHeight;
        [self.detialView refreshWidth];
    }
    
}

-(NSMutableAttributedString*)changeStr:(NSString*)str{
    NSString *str0 = @"已连续打卡 ";
    NSMutableAttributedString *pStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@%@天 ｜ 累计岛币",str0,str]];
    [pStr addAttribute:NSForegroundColorAttributeName value:KFontGrayColor range:NSMakeRange(0,pStr.length)];
    [pStr addAttribute:NSForegroundColorAttributeName value:KFontBlackColor range:NSMakeRange(str0.length,str.length)];
    [pStr addAttribute:NSForegroundColorAttributeName value:KLineColor range:NSMakeRange(str0.length+str.length+2,1)];
    [pStr addAttribute:NSFontAttributeName value:JPUIFontPingFangSCRegular(13) range:NSMakeRange(0,pStr.length)];
    [pStr addAttribute:NSFontAttributeName value:JPUIFontInterMedium(16) range:NSMakeRange(str0.length,str.length)];
    return pStr;
}

#pragma mark - 明细
-(void)showDetialView{
    if(!self.detialView){
        self.detialView = [[CoinDetialView alloc]initWithFrame:CGRectMake(0, 0,self.currentAvailableWidth,self.currentAvailableHeight)];
    }
    self.detialView.currentAvailableWidth = self.currentAvailableWidth;
    self.detialView.currentAvailableHeight = self.currentAvailableHeight;
    UIWindow *keyWindow = KEY_WINDOW;
    if(keyWindow){
        [keyWindow addSubview:self.detialView];
    }
}

-(void)setUserModel:(MineUserModel *)userModel{
    _userModel = userModel;
    self.balanceLabel.text = [self insetDouhao:userModel.goldNumber?userModel.goldNumber:@"0"];
    self.dayLabel.attributedText = [self changeStr:userModel.signCount?userModel.signCount:@"0"];
    CGSize d_size = [self.dayLabel sizeThatFits:CGSizeMake(MAXFLOAT,19)];
    [self.dayLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(d_size.width+6);
    }];
    __weak typeof(self)self_weak_ = self;
    [self.coinImageView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self_weak_.dayLabel.mas_right);
    }];
    self.coinLabel.text = userModel.addGoldNumber;
    [self.coinLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self_weak_.coinImageView.mas_right).offset(3);
    }];
}

-(void)refreshSignCountProgress{
    
    NSInteger day = self.userModel.signCount?self.userModel.signCount.integerValue:0;
    CGFloat day_w = 0;
    CGFloat p_w = (self.currentAvailableWidth-64)/self.arr.count;
    NSInteger totalDay = [self getNumberOfDaysInCurrentYear];
    if(!totalDay){
        totalDay = 365;
    }
    for(NSInteger i = 0;i < self.arr.count;i++){
        TaskListModel *model = self.arr[i];
        //进度长度
        NSString *dayname = model.name;
        dayname = [dayname stringByReplacingOccurrencesOfString:@"" withString:@"天"];
        if(day > dayname.integerValue){
            if((i+1) < self.arr.count){
                //在两个点之间
                TaskListModel *model2 = self.arr[i+1];
                NSString *dayname2 = model2.name;
                dayname2 = [dayname2 stringByReplacingOccurrencesOfString:@"" withString:@"天"];
                if(dayname2.integerValue > day){
                    //取最后一个大于这个天数的点 载加上离下一个点的距离
                    CGFloat midday = p_w/(dayname2.integerValue-dayname.integerValue);
                    day_w = p_w/2.0+i*p_w+midday*(day-dayname.integerValue);
                }
            }else{
                //最后一个点
                if(i == self.arr.count-1){
                    NSInteger lastday = totalDay-dayname.integerValue;
                    CGFloat midday = (p_w/2.0)/lastday;
                    if(day >= dayname.integerValue){
                        day_w = p_w/2.0+p_w*i+midday*(day-dayname.integerValue);
                    }else{
                        day_w = p_w+p_w*i;
                    }
                }
            }
        }else if (day == dayname.integerValue){
            //刚好在点上的天数
            if(i == 0){
                day_w = p_w/2.0;
            }else{
                day_w = p_w/2.0+p_w*i;
            }
        }else{
            if(i == 0&&day < dayname.integerValue){
                //不到第一个点的天数
                CGFloat midday = (p_w/2.0)/dayname.integerValue;
                day_w = midday*day;
            }
        }
    }
    if(day_w > (self.currentAvailableWidth-64)){
        day_w = self.currentAvailableWidth-64;
    }
    [self.progressLine mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(day_w);
    }];
}

-(void)createYearTaskUI{
    if(self.isCreate == YES){
        [self refreshSignCountProgress];
    }else{
        CGFloat w45 = [DeviceUtils getAdaptedWidthForDesignWidth:45 andCurrentAvailableWidth:self.currentAvailableWidth];
        CGFloat w11 = [DeviceUtils getAdaptedWidthForDesignWidth:11 andCurrentAvailableWidth:self.currentAvailableWidth];
        CGFloat p_w = (self.currentAvailableWidth-64)/self.arr.count;
        UIView *linebg = [[UIView alloc]init];
        linebg.backgroundColor = KLineColor;
        linebg.layer.cornerRadius = 2;
        linebg.layer.masksToBounds = YES;
        [self.topBgImageView2 addSubview:linebg];
        [linebg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(@4);
            make.left.mas_equalTo(@16);
            make.right.mas_equalTo(@-16);
            make.bottom.mas_equalTo(-w45);
        }];
        
        NSInteger day = self.userModel.signCount?self.userModel.signCount.integerValue:0;
        CGFloat day_w = 0;
        
        self.progressLine = [[UIView alloc]init];
        self.progressLine.backgroundColor = KThemeColor;
        self.progressLine.layer.cornerRadius = 2;
        self.progressLine.layer.masksToBounds = YES;
        [self.topBgImageView2 addSubview:self.progressLine];
        [self.progressLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(@16);
            make.height.mas_equalTo(@4);
            make.centerY.mas_equalTo(linebg);
            make.width.mas_equalTo(day_w);
        }];
        
        CGFloat p_left = 16+p_w/2.0-6;
        NSInteger totalDay = [self getNumberOfDaysInCurrentYear];
        if(!totalDay){
            totalDay = 365;
        }
        for(NSInteger i = 0;i < self.arr.count;i++){
            TaskListModel *model = self.arr[i];
            //进度长度
            NSString *dayname = model.name;
            dayname = [dayname stringByReplacingOccurrencesOfString:@"" withString:@"天"];
            if(day > dayname.integerValue){
                if((i+1) < self.arr.count){
                    //在两个点之间
                    TaskListModel *model2 = self.arr[i+1];
                    NSString *dayname2 = model2.name;
                    dayname2 = [dayname2 stringByReplacingOccurrencesOfString:@"" withString:@"天"];
                    if(dayname2.integerValue > day){
                        //取最后一个大于这个天数的点 载加上离下一个点的距离
                        CGFloat midday = p_w/(dayname2.integerValue-dayname.integerValue);
                        day_w = p_w/2.0+i*p_w+midday*(day-dayname.integerValue);
                    }
                }else{
                    //最后一个点
                    if(i == self.arr.count-1){
                        NSInteger lastday = totalDay-dayname.integerValue;
                        CGFloat midday = (p_w/2.0)/lastday;
                        if(day >= dayname.integerValue){
                            day_w = p_w/2.0+p_w*i+midday*(day-dayname.integerValue);
                        }else{
                            day_w = p_w+p_w*i;
                        }
                    }
                }
            }else if (day == dayname.integerValue){
                //刚好在点上的天数
                if(i == 0){
                    day_w = p_w/2.0;
                }else{
                    day_w = p_w/2.0+p_w*i;
                }
            }else{
                if(i == 0&&day < dayname.integerValue){
                    //不到第一个点的天数
                    CGFloat midday = (p_w/2.0)/dayname.integerValue;
                    day_w = midday*day;
                }
            }
            
            UIView *point = [[UIView alloc]init];
            if(day >= dayname.integerValue){
                point.backgroundColor = KThemeColor;
            }else{
                point.backgroundColor = KLineColor;
            }
            point.layer.cornerRadius = 6;
            point.layer.masksToBounds = YES;
            point.layer.borderColor = [UIColor whiteColor].CGColor;
            point.layer.borderWidth = 2.0;
            point.tag = 233+i;
            [self.topBgImageView2 addSubview:point];
            [point mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.height.mas_equalTo(@12);
                make.centerY.mas_equalTo(linebg);
                make.left.mas_equalTo(p_left);
            }];
            p_left = p_left+p_w;
            
            UILabel *pTitleLabel = [[UILabel alloc]init];
            pTitleLabel.font = JPUIFontInterRegular(14);
            pTitleLabel.textColor = KFontGrayColor;
            pTitleLabel.textAlignment = NSTextAlignmentCenter;
            pTitleLabel.text = model.name;
            [self.topBgImageView2 addSubview:pTitleLabel];
            [pTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(@18);
                make.bottom.mas_equalTo(linebg.mas_top).offset(-w11);
                make.centerX.mas_equalTo(point);
                make.width.mas_equalTo(p_w-8);
            }];
            
            UIView *pview = [[UIView alloc]init];
            [self.topBgImageView2 addSubview:pview];
            [pview mas_makeConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(@16);
                make.width.mas_equalTo(p_w-8);
                make.centerX.mas_equalTo(point);
                make.top.mas_equalTo(linebg.mas_bottom).offset(w11);
            }];
            UILabel *pcoinLabel = [[UILabel alloc]init];
            pcoinLabel.font = JPUIFontInterMedium(13);
            pcoinLabel.textColor = KFontBlackColor;
            pcoinLabel.textAlignment = NSTextAlignmentCenter;
            pcoinLabel.text = model.gold_number;
            [self.topBgImageView2 addSubview:pcoinLabel];
            CGFloat pc_w = [self getStringWidthWithText:pcoinLabel.text font:pcoinLabel.font viewHeight:16];
            [pcoinLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.mas_equalTo(pc_w+2);
                make.centerX.mas_equalTo(pview).offset(6);
                make.height.mas_equalTo(@16);
                make.centerY.mas_equalTo(pview);
            }];
            UIImageView *pcoinImageView = [[UIImageView alloc]init];
            pcoinImageView.image = [UIImage imageNamed:@"mine_point_point_logo"];
            [self.topBgImageView2 addSubview:pcoinImageView];
            [pcoinImageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.mas_equalTo(pcoinLabel.mas_left);
                make.height.width.mas_equalTo(@12);
                make.centerY.mas_equalTo(pcoinLabel);
            }];
            
        }
        if(day_w > (self.currentAvailableWidth-64)){
            day_w = self.currentAvailableWidth-64;
        }
        [self.progressLine mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(day_w);
        }];
        self.isCreate = YES;
    }
    
}

#pragma mark - 获取全年天数
-(NSInteger)getNumberOfDaysInCurrentYear{
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSInteger currentYear = [calendar component:NSCalendarUnitYear fromDate:[NSDate date]];
    NSDateComponents *startDateComponents = [[NSDateComponents alloc] init];
    startDateComponents.year = currentYear;
    startDateComponents.month = 1;
    startDateComponents.day = 1;
    NSDate *startDate = [calendar dateFromComponents:startDateComponents];
    NSDateComponents *endDateComponents = [[NSDateComponents alloc] init];
    endDateComponents.year = currentYear;
    endDateComponents.month = 12;
    endDateComponents.day = 31;
    NSDate *endDate = [calendar dateFromComponents:endDateComponents];
    NSDateComponents *numberOfDaysComponents = [calendar components:NSCalendarUnitDay
                                                          fromDate:startDate
                                                            toDate:endDate
                                                           options:0];
    NSInteger numberOfDays = numberOfDaysComponents.day + 1;
    return numberOfDays;
    
}

#pragma mark - 资产添加逗号
- (NSString *)insetDouhao:(NSString *)str{
    NSString *intStr;
    NSString *floStr;
    if ([str containsString:@"."]) {
        NSRange range = [str rangeOfString:@"."];
         floStr = [str substringFromIndex:range.location];
         intStr = [str substringToIndex:range.location];
    }else{
        floStr = @"";
        intStr = str;
    }
    if (intStr.length <=3) {
        return [intStr stringByAppendingString:floStr];
    }else{
        NSInteger length = intStr.length;
        NSInteger count = length/3;
        NSInteger y = length%3;
 
        NSString *tit = [intStr substringToIndex:y] ;
        NSMutableString *det = [[intStr substringFromIndex:y] mutableCopy];
 
        for (int i =0; i < count; i ++) {
            NSInteger index = i + i *3;
            [det insertString:@","atIndex:index];
        }
        if (y ==0) {
            det = [[det substringFromIndex:1]mutableCopy];
        }
        intStr = [tit stringByAppendingString:det];
        return [intStr stringByAppendingString:floStr];
    }
}



@end
